package basicservicelogic

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
	"property_service/dao/model"
	"property_service/dao/query"
	"property_service/internal/svc"
	"property_service/property_service"
)

type StatusAuditUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewStatusAuditUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *StatusAuditUserLogic {
	return &StatusAuditUserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 用户审核状态 返回是否为最终状态
func (l *StatusAuditUserLogic) StatusAuditUser(in *property_service.AuditUser) (*property_service.ResponseStatus, error) {
	//查看用户可操作步骤，如果为空则表示没有操作权限   通过status_group，userid以及status_code查询是否有权限
	// 定义一个状态审核授权表模型对象切片用于存储查询结果
	var data []*model.StatusAuthorization
	// 获取状态审核授权表模型的查询构造器
	m := query.Use(l.svcCtx.Gorm).StatusAuthorization
	// 构建查询条件：根据租户ID查询
	do := m.WithContext(l.ctx).Where(m.TenantID.Eq(in.TenantID))
	//查询条件需要判断是否打开状态
	do = do.Where(m.Status.Eq("1"))
	//赋值查询条件并且判断长度是否为一
	if in.StatusCode != "" {
		do = do.Where(m.StatusCode.Eq(in.StatusCode)) // 状态代码
	} else {
		return &property_service.ResponseStatus{
			Code:    404,
			Message: "请传输状态代码StatusCode",
		}, nil
	}
	if in.StatusGroup != "" {
		do = do.Where(m.StatusGroup.Eq(in.StatusGroup)) // 状态分组
	} else {
		return &property_service.ResponseStatus{
			Code:    404,
			Message: "请传输状态分组StatusGroup",
		}, nil
	}
	if in.UserID > 0 {
		do = do.Where(m.UserID.Eq(in.UserID)) // 用户id
	} else {
		return &property_service.ResponseStatus{
			Code:       404,
			Message:    "请传输状态用户IDUserID",
			StatusCode: "",
		}, nil
	}
	//判断用户操作的步骤是否为0，如果本身就为0，则不需要审核

	// 定义一个变量用于存储总记录数
	var count int64

	// 如果不需要分页，查询所有记录
	data, _ = do.Find()
	// 设置总记录数为查询结果的长度
	count = int64(len(data))

	// 将查询结果转换为响应对象
	if count < 1 {
		return &property_service.ResponseStatus{
			Code:       404,
			Message:    "对不起，你没有操作权限",
			StatusCode: "",
		}, nil
	} else if count == 1 {
		//如果等于1则表示用户有权限进行此操作
		// 定义一个状态表模型对象切片用于存储查询结果
		var statusdisposition *model.StatusDisposition
		// 获取状态表模型的查询构造器
		m := query.Use(l.svcCtx.Gorm).StatusDisposition
		// 构建查询条件：根据租户ID查询
		do := m.WithContext(l.ctx).Where(m.TenantID.Eq(in.TenantID)) // 租户ID
		do = do.Where(m.StatusCode.Eq(in.StatusCode)).Where(m.StatusGroup.Eq(in.StatusGroup))
		//查询符合条件的根据StatusCode，StatusGroup以及Status
		statusdisposition, _ = do.First()
		if statusdisposition == nil {
			return &property_service.ResponseStatus{
				StatusCode: "未找到指定状态定义",
				Code:       404,
				Message:    "未找到指定状态定义",
			}, nil
		}
		//定义返回值
		var srt int64
		//获取用户操作的类型  1同意 / 0拒绝
		if in.Status == "1" {
			srt = statusdisposition.IfAgree
		} else if in.Status == "0" {
			srt = statusdisposition.IfRefuse
		}
		if srt == 0 {
			DB := l.svcCtx.Gorm
			StatusName := ""
			DB.Raw("SELECT status_name FROM status_disposition WHERE tenant_id= ? AND status_code = ?  AND  status_group = ?", in.TenantID, srt, statusdisposition.StatusGroup).Scan(&StatusName)

			if StatusName == "" {
				return &property_service.ResponseStatus{
					StatusCode: "",
					StatusName: "",
					Code:       404,
					Message:    "未找到该状态",
				}, nil
			}

			//判断是否为0，如果为0则提示本步骤的提示词
			return &property_service.ResponseStatus{
				StatusCode: "0",
				Code:       200,
				StatusName: StatusName,
				Message:    statusdisposition.StatusEndDescription,
			}, nil
		} else {
			DB := l.svcCtx.Gorm
			//设置消息提示词
			msg := ""
			DB.Raw("SELECT status_end_description FROM status_disposition WHERE tenant_id= ? AND status_code = ?  AND  status_group = ?", in.TenantID, srt, statusdisposition.StatusGroup).Scan(&msg)
			StatusCode := fmt.Sprintf("%d", srt)
			StatusName := ""
			DB.Raw("SELECT status_name FROM status_disposition WHERE tenant_id= ? AND status_code = ?  AND  status_group = ?", in.TenantID, srt, statusdisposition.StatusGroup).Scan(&StatusName)
			if StatusName == "" {
				return &property_service.ResponseStatus{
					StatusCode: "",
					StatusName: "",
					Code:       404,
					Message:    "未找到该状态",
				}, nil
			}
			//如果不为0，获取指定步骤的消息提示词
			return &property_service.ResponseStatus{
				StatusCode: StatusCode,
				StatusName: StatusName,
				Code:       200,
				Message:    msg,
			}, nil
		}
	}

	return &property_service.ResponseStatus{}, nil
}
